package com.survivor.game;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.survivor.activity.TextureLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.FontMetrics;
import android.opengl.GLUtils;
import android.util.Printer;

public class GameText {
	
	private float[] verCoord = 
	{
		-.5f, -.5f, 0,
		.5f, -.5f, 0,
		-.5f, .5f, 0,
		.5f, .5f, 0
	};
	
	private float[] texCoord =
	{
		0, 1,
		1, 1,
		0, 0,
		1, 0
	};
	
	private FloatBuffer verBuffer, texBuffer;
	
	private Paint mPaint = new Paint();
	private Typeface tf;
	
	private int[] textures = new int[2];
	private List<Bitmap> bmpArray = new ArrayList<Bitmap>();
	
	private String loseStr = "GAME OVER";
	private String passStr = "PASS";
	
	
	
	public GameText(Context context) {
		// TODO Auto-generated constructor stub
		tf = Typeface.createFromAsset(context.getAssets(), "fonts/Plok.ttf");
		mPaint.setFakeBoldText(true);
		mPaint.setTypeface(tf);
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.WHITE);
		
		verBuffer = makeFloatBuffer(verCoord);
		texBuffer = makeFloatBuffer(texCoord);
		
		bmpArray.add(createBmp(loseStr, 512, 256));
		bmpArray.add(createBmp(passStr, 512, 256));
	}
	
	private FloatBuffer makeFloatBuffer(float[] src)
	{
		ByteBuffer bb = ByteBuffer.allocateDirect(src.length * 4).order(ByteOrder.nativeOrder());
		FloatBuffer	fb = bb.asFloatBuffer();
		fb.put(src);
		fb.position(0);
		
		return fb;
	}
	
	private float angle = 0;
	private float scale = 0;
	
	public void loadTextures(GL10 gl)
	{
		TextureLoader.loadTextures(gl, bmpArray, textures);
	}
	
	public void drawLost(GL10 gl)
	{
		gl.glLoadIdentity();
		
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verBuffer);
		gl.glRotatef(angle, 0, 0, 1);
		gl.glScalef(scale, scale, 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glColor4f(0, 0.3764f, 0.035f, 1f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		if(angle < 361)
		{
			angle += 2;
			scale += .008f;
		}
			
	}
	
	private float passScale = 0;
	
	public void drawPass(GL10 gl)
	{
		gl.glLoadIdentity();
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verBuffer);
		gl.glColor4f(0.2784f, 0.2196f, 0.09f, 1f);
		gl.glTranslatef(0.3f, 0, 0);
		gl.glScalef(passScale, passScale, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		
		if(passScale < 1.5f)
			passScale += 0.08f;
	}
	
	private Bitmap createBmp(String s, int bmpWidth, int bmpHeight)
	{
		Bitmap bmp = Bitmap.createBitmap(bmpWidth, bmpHeight,
				Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.setBitmap(bmp);
		mPaint.setTextSize(50);
		canvas.drawText(s, 0, bmpHeight / 2, mPaint);
		return bmp;
	}
	
	
	
	

}
